package com.tianqu.tms.www.transfer.controller;

import java.net.URLDecoder;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.webservice.weichat.api.WeChatMqApi;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferAcceptedLCInfoBo;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinanceInfo;
import com.tianqu.tms.entity.TransferProcessHistory;

/**
 * 库存车辆受理-更新数据 controller
 * @author mufeiyu
 *
 */
public class CarStockAcceptTransferInfoController extends TmsController {
	private Logger logger = Logger.getLogger(CarStockAcceptTransferInfoController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			
			String basicCarId = uc.getParameter("basicCarId");
			String carOwner = uc.getParameter("carOwner");
			carOwner = URLDecoder.decode(carOwner,"utf-8");
			String checkPriceStr = uc.getParameter("checkPrice");
			
			TmsUserProfile profile = uc.getUserProfile();
			TransferAcceptedInfo transferAccept = TransferAcceptedInfo.getInstance();
			
			//过户业务基本信息
			transferAccept.setBasicCarId(basicCarId);//车辆编号
			transferAccept.setCarOwner(carOwner);//车主姓名
			transferAccept.setLc_printNum(1);//流程表打印次数
			transferAccept.setLc_printUserId(profile.getUniqueId());//流程表打印操作员
			transferAccept.setStatus(2);//过户状态
			transferAccept.setUpdateUserId(profile.getUniqueId());//更新用户
			
			//更新 过户业务基本信息表（tms_transfer_accepted_info）
			transferAccept.updateTransferAcceptedLCByBasicCarId(transferAccept);
			
			TransferAcceptedInfo transferAccept1 = TransferAcceptedInfo.getInstance();
			transferAccept1 = transferAccept1.findTransferInfoByBasicCarId(basicCarId);
			String serial = transferAccept1.getSerial();
			
			//过户流程操作信息			
			TransferProcessHistory transferProcess = TransferProcessHistory.getInstance();			
			transferProcess.setSerial(serial);
			transferProcess.setBusinessType(2);
			transferProcess.setInsertUserId(profile.getUniqueId());	
			if(StringUtil.isNullOrBlank(checkPriceStr)) transferProcess.setReason("库存车辆受理打印流程表时，系统自动使用库存登记预缴费用进行缴费打表");//modify by huangwei 20170501
			//插入 过户流程操作记录表（tms_transfer_process_history）
			transferProcess.insertHisInfoAtAssess(transferProcess);
			
			if(!StringUtil.isNullOrBlank(checkPriceStr)){
				//过户业务的财务收费信息
				TransferFinanceInfo transferFinance = TransferFinanceInfo.getInstance();
				transferFinance.setSerial(serial);
				transferFinance.setCheckPrice(Integer.valueOf(checkPriceStr));
				transferFinance.setCheckPriceUserId(profile.getUniqueId());
				transferFinance.setCheckPriceChargeType(1);

				//更新 tms_transfer_finance_info（过户业务的财务收费信息表）
				transferFinance.updateFinanceInfoAtWXByCheckPrice(transferFinance);
			}
			
			uc.setAttribute("checkSerial", serial);
			
			controller.commit();
			
			//接口编号：402；【TMS_web】库存车辆受理 - 点击打印流程表 =》【TMS_app】同步
			TransferAcceptedLCInfoBo acceptedLCInfoBo = new TransferAcceptedLCInfoBo();
			acceptedLCInfoBo.setSerial(serial);
			acceptedLCInfoBo.setCarOwner(transferAccept1.getCarOwner());
			acceptedLCInfoBo.setDetailStatus(transferAccept1.getStatus());
			

			WeChatMqApi.getInstance().call(acceptedLCInfoBo);
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
}
